package com.prp.gwt.client.widgets.user.friend;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.friend.FriendListener;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.vo.UserList;

public class FriendListPanel extends Composite {

	private static FriendListPanel instance;
	private UserList userList;
	private UserList resultList;
	public static final int NUM_PER_PAGE = 8;
	private int index;
	HTML head = new HTML("<a>首页</a>");
	HTML next = new HTML("<a>下一页</a>");
	HTML last = new HTML("<a>上一页</a>");
	HTML tail = new HTML("<a>尾页</a>");
	Image refreshImage = new Image(GWT.getModuleBaseURL() + "loading.gif");
	protected User sender;
	protected User owner;
	private VerticalPanel innerPanel = new VerticalPanel();
	private VerticalPanel vPanel = new VerticalPanel();
	private Image loadImage = new Image(GWT.getModuleBaseURL() + "loading.gif");
	private HTML title;
	public TextBox searchBox = new TextBox();
	public HTML searchImage = new HTML("<a><img src=\""
			+ GWT.getModuleBaseURL() + "icon_search.png\"/>" + "<a>");
	public HTML myFriends = new HTML("<a>返回我的邻友</a>");

	public static FriendListPanel getInstance() {
		return instance;
	}

	public FriendListPanel() {
		instance = this;
		initWidget(innerPanel);
		title = new HTML("我的邻友");
		title.addStyleName("friendList-title");
		innerPanel.add(title);
		innerPanel.add(loadImage);
		innerPanel.add(vPanel);
		vPanel.addStyleName("page-frame");
		searchBox.setVisibleLength(10);
		searchBox.addStyleName("friendList-searchBox");
		myFriends.setVisible(false);
		myFriends.addStyleName("friend-item-text");
		head.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(1);
			}
		});
		tail.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(getPageCount());
			}
		});
		next.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index + 1);
			}
		});
		last.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index - 1);
			}
		});
		searchImage.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				loadResult();
			}
		});
		searchBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == 13) {
					loadResult();
				} else if (searchBox.getText().isEmpty()
						&& userList != resultList) {
					loadResult();
				}
			}
		});
		myFriends.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				myFriends.setVisible(false);
				init();
				FriendListener.getInstance().getFriendList(-1);
			}
		});
		innerPanel.addStyleName("page-frame");
		vPanel.addStyleName("page-frame");
	}

	public void init() {
		vPanel.clear();
		loadImage.setVisible(true);
		title.setText("邻友");
	}

	public void loadResult() {
		String userName = searchBox.getText();
		resultList = new UserList();
		for (int i = 0; i < userList.size(); i++)
			if (userList.get(i).getUsername().contains(userName))
				resultList.addUser(userList.get(i));
		toPage(1);
	}

	public void load(UserList userList, String title, boolean flag) {
		loadImage.setVisible(false);
		this.userList = userList;
		this.resultList = userList;
		this.owner = userList.getOwner();
		this.sender = userList.getSender();
		vPanel.clear();
		this.title.setText(title);
		if(flag)
			myFriends.setVisible(false);
		else
			myFriends.setVisible(true);
		toPage(1);
	}

	public void toPage(int index) {
		if (index > 0 && index <= getPageCount())
			this.index = index;
		else
			return;
		vPanel.clear();
		loadPagePanel();
		loadFriendItem();
	}

	public void loadFriendItem() {
		int start = (index - 1) * NUM_PER_PAGE;
		int end = Math.min(index * NUM_PER_PAGE, resultList.size());
		for (int i = start; i < end; i++)
			vPanel.add(new FriendItem(resultList.get(i)));
	}

	private void loadPagePanel() {
		vPanel.addStyleName("friendList-panel");
		HorizontalPanel panel = new HorizontalPanel();
		panel.setStyleName("page-panel");
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			hPanel.add(searchBox);
			hPanel.add(searchImage);
			hPanel.add(myFriends);
			panel.add(hPanel);
		}
		HorizontalPanel innerPanel = new HorizontalPanel();
		int start = 0;
		int end = 0;
		if (getPageCount() <= 5 || index < 3) {
			start = 1;
			end = Math.min(5, getPageCount());
		} else if (getPageCount() - index < 3) {
			start = getPageCount() - 4;
			end = getPageCount();
		} else {
			start = index - 2;
			end = index + 2;
		}
		innerPanel.add(head);
		innerPanel.add(last);
		if (index == 1) {
			head.removeStyleName("page");
			last.removeStyleName("page");
			head.addStyleName("page-disable");
			last.addStyleName("page-disable");
		} else {
			head.removeStyleName("page-disable");
			last.removeStyleName("page-disable");
			head.addStyleName("page");
			last.addStyleName("page");
		}
		for (int i = start; i <= end; i++) {
			if (i == index) {
				HTML l = new HTML("<a>" + String.valueOf(i) + "</a>");
				l.addStyleName("page-selected");
				innerPanel.add(l);
				continue;
			}
			HTML label = new HTML("<a>" + String.valueOf(i) + "</a>");
			label.setStylePrimaryName("page");
			label.setStylePrimaryName("page");
			innerPanel.add(label);
			final int j = i;
			label.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					toPage(j);
				}
			});
		}
		innerPanel.add(next);
		innerPanel.add(tail);
		if (index == getPageCount()) {
			next.removeStyleName("page");
			tail.removeStyleName("page");
			next.addStyleName("page-disable");
			tail.addStyleName("page-disable");
		} else {
			next.removeStyleName("page-disable");
			tail.removeStyleName("page-disable");
			next.addStyleName("page");
			tail.addStyleName("page");
		}
		panel.add(innerPanel);
		panel.setCellHorizontalAlignment(innerPanel,
				HorizontalPanel.ALIGN_RIGHT);
		vPanel.add(panel);
	}

	public int getPageCount() {
		return (resultList.size() - 1) / NUM_PER_PAGE + 1;
	}

}
